<?php

namespace app\api\controller;

use app\api\model\User as UserModel;

/**
 * 用户管理
 * Class User
 * @package app\api
 */
class User extends Controller
{
    /**
     * 用户自动登录
     * @return array
     * @throws \app\common\exception\BaseException
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function login()
    {
        $model = new UserModel;
        return $this->renderSuccess([
            'user_id' => $model->login($this->request->post()),
            'token' => $model->getToken()
        ]);
    }
    public function phoneNumber(){
        $model = new UserModel;
        $post = $this->request->post();
        return $this->renderSuccess([
            'userInfo' => $model->phoneNumber($this->request->post()),
            'token' => $post['tokens']
        ]);
    }
    public function phoneSupplier(){
        $model = new UserModel;
        $user = $this->getUser();
        $post = $this->request->post();
        return $this->renderSuccess([
            'userInfo' => $model->phoneSupplier($this->request->post(),$user)
        ]);
    }
    public function huoKuanLists(){
        $user = $this->getUser();
        $lists = db('supplier_huokuan')->where(['sid'=>$user['supplier_id'],'uid'=>$user['user_id']])->order('id desc')->select()->toArray();
        foreach ($lists as $k=>$v){
            $lists[$k]['picLists'] = db('supplier_pic')->where(['hkid'=>$v['id']])->select();
        }
        return $this->renderSuccess([
            'lists' => $lists
        ]);
    }
    public function addImg(){
        $post = $this->request->post();
        $user = $this->getUser();
        $data['sid'] = $user['supplier_id'];
        $data['uid'] = $user['user_id'];
        $data['nickName'] = $user['nickName'];
        $data['openid'] = $user['openid'];
        $data['open_id'] = $user['open_id'];
        $data['money'] = $post['money'];
        $data['title'] = date('Y-m-d H:i:s').'送货-货款';
        $data['add_date'] = date('Y-m-d H:i:s');
        $data['add_time'] = time();
        $huokuanId = db('supplier_huokuan')->insert($data,false,true);
        $imgs = explode(',',$post['imgs']);
        for($i = 0; $i < count($imgs); $i++){
            $media = 'https://shijiebuy.oss-cn-beijing.aliyuncs.com/'.$imgs[$i];
            $picData['hkid'] = $huokuanId;
            $picData['media'] = $media;
            db('supplier_pic')->insert($picData);
        }
        return $this->renderSuccess('发货成功，等待打款');
    }
    public function regSupplier(){
        $model = new UserModel;
        $user = $this->getUser();
        $post = $this->request->post();
        return $this->renderSuccess([
            'userInfo' => $model->regSupplier($this->request->post(),$user)
        ]);
    }

    /**
     * 当前用户详情
     * @return array
     * @throws \app\common\exception\BaseException
     * @throws \think\exception\DbException
     */
    public function detail()
    {
        // 当前用户信息
        $userInfo = $this->getUser();
        return $this->renderSuccess(compact('userInfo'));
    }
    public function getBill(){
        // 当前用户信息
        $userInfo = $this->getUser();
//        $userInfo['user_id'] = 11994;
        $info['kaishi'] = 1;
        $userBillInfo = db('user_log_bill')->where(['uid'=>$userInfo['user_id']])->find();
        if(empty($userBillInfo)) {
            //线下付款金额
            $xiaPayPrice = db('pay_order')->where(['pay_status' => 20, 'user_id' => $userInfo['user_id']])->sum('pay_price');
            //线上付款金额
            $shangPayPrice = db('order')->where(['order_status' => 30, 'pay_status' => 20, 'user_id' => $userInfo['user_id']])->sum('pay_price');
            $startdate = strtotime($userInfo['create_time']);
            $enddate = strtotime(date('Y-m-d'));
            $days = round(($enddate - $startdate) / 3600 / 24);
            $info['days'] = $days;
            $info['xiaPayPrice'] = $xiaPayPrice;
            $info['shangPayPrice'] = $shangPayPrice;
            $info['totalPrice'] = $xiaPayPrice + $shangPayPrice;
            $info['status'] = 0; //未领取
            $result = $this->jiang($info['totalPrice']);

            $data['uid'] = $userInfo['user_id'];
            $data['days'] = $days;
            $data['xia_pay_price'] = $xiaPayPrice;
            $data['shang_pay_price'] = $shangPayPrice;
            $data['total_price'] = $info['totalPrice'];
            $data['title'] = $result['title'];
            $data['images'] = $result['images'];
            $data['create_time'] = time();
            $data['update_time'] = time();
            $data['wxapp_id'] = 10001;
            db('user_log_bill')->insert($data);
        }else{
            $info['days'] = $userBillInfo['days'];
            $info['xiaPayPrice'] = $userBillInfo['xia_pay_price'];
            $info['shangPayPrice'] = $userBillInfo['shang_pay_price'];
            $info['totalPrice'] = $userBillInfo['total_price'];
            $info['status'] = $userBillInfo['status'];
        }
        return $this->renderSuccess(compact('info'));
    }
    public function getJiangPin(){
        // 当前用户信息
        $userInfo = $this->getUser();
        $userBillInfo = db('user_log_bill')->where(['uid'=>$userInfo['user_id']])->find();
        return $this->renderSuccess(compact('userBillInfo'));
    }
    public function lingjiang(){
        $param = $this->request->param();
        $data['username'] = $param['username'];
        $data['mobile'] = $param['mobile'];
        $data['status'] = 1;
        $result = db('user_log_bill')->where(['id'=>$param['id']])->update($data);
        return $this->renderSuccess('领取成功！');
    }
    public function jiang($totalPrice){
        $jiang = array(
            '0'=>[
                'title'=>'破壁机',
                'img'=>'1.jpg'
            ],
            '1'=>[
                'title'=>'30cm 岳扬电热锅',
                'img'=>'2.jpg'
            ],
            '2'=>[
                'title'=>'28cm 厨尚佳三层蒸锅',
                'img'=>'3.jpg'
            ],
            '3'=>[
                'title'=>'32cm 炒锅',
                'img'=>'4.jpg'
            ],
            '4'=>[
                'title'=>'24cm 电煮锅',
                'img'=>'5.jpg'
            ],
            '5'=>[
                'title'=>'22cm 电煮锅',
                'img'=>'5.jpg'
            ],
            '6'=>[
                'title'=>'多彩快餐杯',
                'img'=>'6.jpg'
            ],
            '7'=>[
                'title'=>'硅胶锅铲或者硅胶汤勺',
                'img'=>'7.jpg'
            ],
            '8'=>[
                'title'=>'饺子盘两个或者',
                'img'=>'8.jpg'
            ],
            '9'=>[
                'title'=>'不锈钢小勺子一个',
                'img'=>'9.jpg'
            ],
            '10'=>[
                'title'=>'水果盘一个',
                'img'=>'10.jpg'
            ],
            '11'=>[
                'title'=>'谢谢参与',
                'img'=>'11.jpg'
            ]
        );
        if($totalPrice >=1 && $totalPrice < 10){
            $title = $jiang[9]['title'];
            $images = 'https://shop.suiguantun.com/bill/images/'.$jiang[9]['img'];
        }elseif($totalPrice >=10 && $totalPrice < 50){
            $title = $jiang[10]['title'];
            $images = 'https://shop.suiguantun.com/bill/images/'.$jiang[10]['img'];
        }elseif($totalPrice >=50 && $totalPrice < 100){
            $title = $jiang[8]['title'];
            $images = 'https://shop.suiguantun.com/bill/images/'.$jiang[8]['img'];
        }elseif($totalPrice >=100 && $totalPrice < 400){
            $title = $jiang[7]['title'];
            $images = 'https://shop.suiguantun.com/bill/images/'.$jiang[7]['img'];
        }elseif ($totalPrice >= 400 && $totalPrice < 800) {
            $title = $jiang[6]['title'];
            $images = 'https://shop.suiguantun.com/bill/images/'.$jiang[6]['img'];
        }elseif ($totalPrice >= 800 && $totalPrice < 1000) {
            $title = $jiang[5]['title'];
            $images = 'https://shop.suiguantun.com/bill/images/'.$jiang[5]['img'];
        }elseif ($totalPrice >= 1000 && $totalPrice < 3000) {
            $title = $jiang[4]['title'];
            $images = 'https://shop.suiguantun.com/bill/images/'.$jiang[4]['img'];
        }elseif ($totalPrice >= 3000 && $totalPrice < 5000) {
            $title = $jiang[3]['title'];
            $images = 'https://shop.suiguantun.com/bill/images/'.$jiang[3]['img'];
        }elseif ($totalPrice >= 5000 && $totalPrice < 8000) {
            $title = $jiang[2]['title'];
            $images = 'https://shop.suiguantun.com/bill/images/'.$jiang[2]['img'];
        }elseif ($totalPrice >= 8000 && $totalPrice < 10000) {
            $title = $jiang[1]['title'];
            $images = 'https://shop.suiguantun.com/bill/images/'.$jiang[1]['img'];
        }elseif ($totalPrice >= 10000) {
            $title = $jiang[0]['title'];
            $images = 'https://shop.suiguantun.com/bill/images/'.$jiang[0]['img'];
        }else{
            $title = $jiang[11]['title'];
            $images = 'https://shop.suiguantun.com/bill/images/'.$jiang[11]['img'];
        }
        return ['title'=>$title,'images'=>$images];
    }

}
